package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.SkillStationsItems;
import com.xmy.cultivate.entity.SkillStationsTeacher;
import com.xmy.cultivate.service.ISkillStationsItemsService;
import com.xmy.cultivate.service.ISkillStationsTeacherService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 岗位老师 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-08-21
 */
@RestController
@RequestMapping("/api/skillStationsTeacher")
public class SkillStationsTeacherController {


    @Autowired
    ISkillStationsTeacherService iSkillStationsTeacherService;
    @GetMapping("/findAll")
    public Result findAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                          @RequestParam(value = "departmentId", required = false,defaultValue = "23") String departmentId, //27：市场培训，23：教学培训
                          @RequestParam(value = "yearPart", required = false) String yearPart,
                          @RequestParam(value = "quarterNum", required = false) String quarterNum,
                          @RequestParam(value = "stationsId", required = false) String stationsId,
                          @RequestParam(value = "subjectsId", required = false) String subjectsId,
                          @RequestParam(value = "teacherId", required = false) String teacherId,
                          @RequestParam(value = "type", required = false,defaultValue = "1") String type
    ){
        IPage<SkillStationsTeacher> iPage = new Page<>(pageNum, pageSize);
        iSkillStationsTeacherService.findAll(iPage, yearPart, quarterNum, departmentId, type, stationsId, teacherId, subjectsId);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));

    }


    @PostMapping("/addSkillStationsTeacher")
    public Result addSkillStationsTeacher(@RequestBody SkillStationsTeacher skillStationsTeacher) {
        QueryWrapper<SkillStationsTeacher> qw = new QueryWrapper<>();
        qw.eq("year_part", skillStationsTeacher.getYearPart());
        qw.eq("quarter_num", skillStationsTeacher.getQuarterNum());
        qw.eq("department_id", skillStationsTeacher.getDepartmentId());
        qw.eq("stations_id", skillStationsTeacher.getStationsId());
        qw.eq("teacher_id", skillStationsTeacher.getTeacherId());
        qw.eq("subjects_id", skillStationsTeacher.getSubjectsId());
        qw.eq("type", skillStationsTeacher.getType());
        long count = iSkillStationsTeacherService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据已存在");
        }
        boolean save = iSkillStationsTeacherService.save(skillStationsTeacher);

        if (!save) {
            return new Result(Code.ERROR, null, "添加失败");
        }
        return new Result(Code.OK, null, "添加成功");
    }

    @PutMapping("/editSkillStationsTeacher")
    public Result editSkillStationsTeacher(@RequestBody SkillStationsTeacher skillStationsTeacher) {
        SkillStationsTeacher skillStationsTeacherOld   = iSkillStationsTeacherService.getById(skillStationsTeacher.getId());
        if (Objects.isNull(skillStationsTeacherOld)){
            return new Result(Code.EXP_ERROR, null, "该数据不存在,编辑失败");
        }
        QueryWrapper<SkillStationsTeacher> qw = new QueryWrapper<>();
        qw.ne("id", skillStationsTeacher.getId());
        qw.eq("year_part", skillStationsTeacher.getYearPart());
        qw.eq("quarter_num", skillStationsTeacher.getQuarterNum());
        qw.eq("department_id", skillStationsTeacher.getDepartmentId());
        qw.eq("stations_id", skillStationsTeacher.getStationsId());
        qw.eq("subjects_id", skillStationsTeacher.getSubjectsId());
        qw.eq("teacher_id", skillStationsTeacher.getTeacherId());
        qw.eq("type", skillStationsTeacher.getType());
        long count = iSkillStationsTeacherService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据已存在");
        }
        boolean update = iSkillStationsTeacherService.updateById(skillStationsTeacher);

        if (!update) {
            return new Result(Code.ERROR, null, "编辑失败");
        }
        return new Result(Code.OK, null, "编辑成功");
    }

    @DeleteMapping("/deleteSkillStationsTeacher")
    public Result deleteSkillShutnum(@RequestBody Map reMap){
        List<String> ids = (List<String>) reMap.get("ids");
        if (ids == null || ids.isEmpty()){
            return new Result(Code.DELETE_ERROR, "", "请选择要删除的id");
        }
        QueryWrapper<SkillStationsTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids);
        boolean remove = iSkillStationsTeacherService.remove(queryWrapper);
        if (remove) {
            return new Result(Code.OK, null, Code.DELETE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.DELETE_ERROR_MSG);
    }
}
